<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    const obj1 = [
      ['2132132','2323','2342342'],
      {name:'张三'},
      {
        function(){
          console.log('函数');
        }
      }
    ]

    const str1 = clone(obj1)
    // console.log(str);
    console.log(str1);

    str1[0][0] = '66666'
    console.log(str1);
    console.log(obj1);

  // 定义判断类型的函数
function checkType(any) {
  return Object.prototype.toString.call(any)
}

  // 定义深拷贝函数
function clone(any){

  if(checkType(any) === 'Object') { // 拷贝对象
    let o = {};
    for(let key in any) {
      o[key] = clone(any[key])
    }
    return o;

  } else if(checkType(any) === 'Array') { // 拷贝数组
    var arr = []
    for(let i = 0,leng = any.length;i<leng;i++) {
      arr[i] = clone(any[i])
    }
    return arr;

  } else if(checkType(any) === 'Function') { // 拷贝函数
    return new Function('return '+any.toString()).call(this)
  } else if(checkType(any) === 'Date') { // 拷贝日期
    return new Date(any.valueOf())
  } else if(checkType(any) === 'RegExp') { // 拷贝正则
    return new RegExp(any)
  } else if(checkType(any) === 'Map') { // 拷贝Map 集合
    let m = new Map()
    any.forEach((v,k)=>{
      m.set(k, clone(v))
    })
    return m
  } else if(checkType(any) === 'Set') { // 拷贝Set 集合
    let s = new Set()
    for(let val of any.values()) { 
      s.add(clone(val))
    }
    return s
  }
  return any;
}


// 深拷贝方法2
const person = {name: 'Sam', age: 31, child: {name: 'Ann'}};

const person2 = JSON.parse(JSON.stringify(person));
person2.name = '刘克'
console.log(person);
console.log(person2);
  </script>
</body>
</html>